package com.calm.clibrary.andswipeback;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.lang.ref.WeakReference;

/**
 * Create by Calm
 * 2020/1/11 15:02
 * 临时的用来展示滑动的view
 */
public class TempViewGroup extends FrameLayout {
    //阴影的宽度
    private static final int SHADOW_WIDTH = 500;
    private Drawable mShadowLeft;
    private int mScreenWidth;
    private WeakReference<View> mPreviousView;
    private int mStatusbarOffset;
    private float mDistanceX;
    public TempViewGroup(Context context){
        super(context);
        //开始颜色 中间颜色 结束颜色
        int colors[] = {0x00000000, 0x17000000, 0x43000000};
        mShadowLeft = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,colors);
        mScreenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
    }
    public void addPreviousView(ViewGroup currentContentView, ViewGroup previousContentView, View previousView){
        mPreviousView = new WeakReference<>(previousView);
        int height = getContext().getResources().getDisplayMetrics().heightPixels;
        int previousViewHeight = previousContentView.getMeasuredHeight();
        int currentViewHeight = currentContentView.getMeasuredHeight();
        boolean isCurrentFull = currentViewHeight == height;
        boolean isPreviousFull = previousViewHeight == height;
        if(isCurrentFull){
            mStatusbarOffset = isPreviousFull ? 0 : height - previousViewHeight;
        }else {
            mStatusbarOffset = isPreviousFull ? -(height - currentViewHeight) : 0;
        }
        MarginLayoutParams previousParams = (MarginLayoutParams) previousView.getLayoutParams();
        previousParams.topMargin = previousParams.topMargin + mStatusbarOffset;
        //把上一个Activity的view添加进来
        addView(previousView,0,previousParams);
        //向左偏移1/3的宽度
        previousView.setTranslationX(-(float) mScreenWidth/3);
    }

    /**
     * 设置view平移
     * @param distancX
     */
    public void setDistancX(float distancX){
        mDistanceX = distancX;
        invalidate();
    }

    /**
     * 将前一页面的view从temp中移除并添加回前一Activity
     * @param previousContentView
     * @param previousView
     */
    public void clearPreviousView(ViewGroup previousContentView,View previousView){
        MarginLayoutParams previousParams = (MarginLayoutParams) previousView.getLayoutParams();
        previousParams.topMargin = previousParams.topMargin - mStatusbarOffset;
        removeView(previousView);
        if(previousContentView != null){
            previousContentView.addView(previousView,previousParams);
        }
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        if(mDistanceX < mScreenWidth && mPreviousView != null && mPreviousView.get() == child){
            int restoreCount = canvas.save();
            canvas.clipRect(0,0,mDistanceX,getMeasuredHeight());
            boolean result = super.drawChild(canvas,child,drawingTime);
            canvas.translate(mDistanceX - SHADOW_WIDTH,0);
            mShadowLeft.setBounds(0,0,SHADOW_WIDTH,getMeasuredHeight());
            mShadowLeft.draw(canvas);
            canvas.restoreToCount(restoreCount);
            return result;
        }
        return super.drawChild(canvas, child, drawingTime);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if(mDistanceX >= mScreenWidth && mPreviousView != null && mPreviousView.get() != null){
            int restoreCount = canvas.save();
            canvas.translate(0,mStatusbarOffset);
            mPreviousView.get().draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    }
}
